<?php
/**
 * 会员相关
 * @author fanzhanao
 *
 */
//error_reporting(E_ALL);
class index_controller_index extends SnsController{
	
	private $cartoon 	= 	'';
	private $chapter	=	'';
	private $comment	=	'';
//	var $right_view = true;
	public function __construct($app, $controller){
		parent::__construct($app, $controller);
		$this->cartoon	=	Model::factory('cartoon_model_data');
		$this->chapter	=	Model::factory('cartoon_model_chapter');
		$this->photo	=	Model::factory('cartoon_model_photo');
        $this->avatar 	= 	Model::factory('member_util_avatar');
        $this->frameView	=	VIEW_PATH.'/index.phtml';        
	}
	
	/**
	 * 首页
	 *
	 */
	public function index_action(){
		/*if($this->checkLogin()){
			$this->_redirect($this->app,$this->controller,'my');
		}*/
		$recom_home = cartoon_util_interface::cacheGetRecomCT(CONST_CT::RECOM_HOME );
//		$this->ret['recom'] = $recom_home;
		if(!self::$login['uid']){			
			$hot	=	cartoon_util_interface::cacheGetRecomCT(CONST_CT::RECOM_HOT );
			$this->ret = array('recom'=>$recom_home,'hot'=>$hot);
		}else{//登陆之后
			$share_type = _('type',2);
			$num = 6;$myuid = self::$login['uid'];
			$share_list	=	array();
			$uid = $exclude =  '';
			switch ($share_type){
				case 1:
					$uid = friends_util_cache::getFriendIds($myuid);
					break;
				case 2:
					$exclude = $myuid;
					break;
				case 3:
					$uid = $myuid;
					break;
				default:
					break;
			}			
			if($share_list = Model::factory('index_model_data')->getShareList($uid,$num,$exclude)){
				foreach ($share_list as &$v){
					$_tmp = $v;
					$v = $this->cartoon->getCartoonInfo($v['res_id']);
					$v['share_uid']	=	$_tmp['uid'];
					$v['shar_time']	=	$_tmp['share_time'];
				}
			}
			$readlog = $this->getReadLog();
			$this->ret = array('recom'=>$recom_home,'share'=>$share_list,'readlog'=>$readlog);
		}
	}
	
	public function my_action(){
		if(!$this->checkLogin()){
			$this->_redirect($this->app,$this->controller,'index');
		}
		$recom = cartoon_util_interface::cacheGetRecomCT(CONST_CT::RECOM_HOME );
		/*$mod = 3;$group_num = ceil(count($recom)/$mod);
		$recomList = array();
		$j = 0;
		for($i=0;$i<count($recom);$i++){
			$recomList[$j][] = $recom[$i];
			if($i>0 && ($i%($mod-1)) == 0){
				++$j;
			}
		}*/
		$uid = self::$login['uid'];
		//获取我订阅的漫画
		$mysub = $this->cartoon->getSubListNew($uid,4);		
		for($i=0;$i<count($mysub);$i++){
			$chapt_list = $this->chapter->getChapterList($mysub[$i]['id'],20);
			$mysub[$i]['chapter']	=	$chapt_list;
		}
		$myfri = friends_util_cache::getFriendIds($uid);
//		$myfri  = array(1,2,3,4,5);
		$friList = $this->cartoon->getViewListNew($myfri,4);
		$chapter_list = array();
		if($friList){
			foreach ($friList as $fl){
				if(!$chapter_list[$fl['id']]){
					$chapter_list[$fl['id']] = cartoon_util_interface::cacheGetChapter($fl['id'],20);
				}else continue;
			}
		}
		$this->ret = array('recom'=>$recom,'mysubscribe'=>$mysub,'fri_ct_list'=>$friList,'chapter_list'=>$chapter_list);
//		print_r($this->ret);exit;
		$this->view = 'index_my';
	}
	
	public function test_action(){
		error_reporting(E_ALL);
		$newList = cartoon_util_interface::cacheCtNewList(4 );
		print_r($newList);exit;
	}

    /**
     * @desc 分类列表
     */
    /*public function cate_list_action(){
        $cateList = cartoon_util_interface::cacheGetCateList();
        $ct_list = array();
        if($cateList){
            while($row = current($cateList)){
                $ct_list[$row['id']] = $this->cartoon->getListByCate($row['id'],5);
            }
        }
        
    }*/

	/**
	 * 漫画浏览页面
	 *
	 */
	public function view_action(){
		$cartoon_id = _('cid');
		$page = _('page');
		/**
		 * 根据漫画id获取漫画信息
		 */
		$cartoon_info = $this->cartoon->getCartoonInfo($cartoon_id);
		if(self::$login['uid'] && $cartoon_info['ct_uid'] != self::$login['uid']){//登陆用户，记录浏览日志
			cartoon_util_log::viewlog($cartoon_id,0,self::$login['uid']);
		}
		$cateList	=	cartoon_util_interface::cacheGetCateList();
		//获取漫画章节信息
//		$chapter_list	=	$this->chapter->getChapterList($cartoon_id);
		/*list($chapter_list,$pageinfo) = MemRow::getPageList(CHAPTER_LIST_IDS, 1,300, array(
				array($this->chapter, 'getChapterListIds', array($cartoon_id)), array($this->chapter, 'getRows')));	*/
		if(self::$login['uid']){//获取读过的记录
			$readList = array();
			$readLog = $this->cartoon->getMyLogs(self::$login['uid']);
			if($readLog){
				foreach ($readLog as $k=>$v){
					$readList[$v['cartoon_id']][] = $v['chapter_id'];
				}
			}
		}
		$chapter_list	=	$this->chapter->getChapterListPages($cartoon_id,$page,4);
		$pages = $this->genPages($chapter_list['INFO'],array('cid'=>$cartoon_id));
		$this->ret = array('cartoon' => $cartoon_info,'chater'=>$chapter_list['ROWS'],'catelist' => $cateList,'pages'=>$pages,'readlist'=>$readList);
		$this->view = 'view_detail';
	}
	
	private function getReadLog(){
		if(self::$login['uid']){//获取读过的记录
			$readList = array();
			$readLog = $this->cartoon->getMyLogs(self::$login['uid']);
			if($readLog){
				foreach ($readLog as $k=>$v){
					$readList[$v['cartoon_id']][] = $v['chapter_id'];
				}
			}
		}
		return $readList;
	}
	/**
	 * 图片浏览
	 *
	 */
	public function view_photo_action(){
		$chapter_id	=	_('cid');
		$cartoon_id	=	_('ctid');
		$ctinfo	=	$this->cartoon->getCartoonInfo($cartoon_id);
		if(!$chapter_id){
			$rs	=	$this->chapter->getChapterList($cartoon_id,1);
			$chapter_id = $rs[0]['id'];
		}
		
		if(self::$login['uid'] && $ctinfo['ct_uid'] != self::$login['uid']){//登陆用户，记录浏览日志
			cartoon_util_log::viewlog($cartoon_id,$chapter_id,self::$login['uid']);
		}
		
		$this->ret = array('ctinfo' =>$ctinfo,'ctid'=>$cartoon_id,'cid'=>$chapter_id);
		$this->view = 'view_photo';
		
	}
	
	
	public function getPhoto_action(){
		$chapter_id	=	_('cid');
		$piclist = $desc = array();
		$rs = $this->photo->getPhotoList($chapter_id);
		if($rs){
			reset($rs);	
			print_r($rs);
			foreach ($rs as $row){
				array_push($piclist,UPLOAD_DOMAIN.'/'.$row['img_url']);
				array_push($desc,$row['img_title']);
			}
		}
		echo implode('|',$piclist).";".implode('|',$desc);
		exit;
	}
	
	/**
	 * 分类浏览
	 *
	 */
	public function cate_list_action(){
		$cateList = cartoon_util_interface::cacheGetCateList();
		//选出推荐的分类
		$recom_list = $ct_list = array();
		$i = 0;
		foreach ($cateList as $row){
			if(2 == $row['cate_flag']){
				$recom_list[$i] = $row;
				$recom_list[$i]['ctlist']	=	$this->cartoon->getListByCate($row['id'],5);
			}else{
				$ct_list[$i]	=	$row;
			}
			++$i;
		}
		$this->ret = array('data'=>$recom_list,'catelist'=>$ct_list);
		
//		print_r($ct_list);
	}
	/**
	 * 根据相片id获取相片所在的位置
	 * Enter description here ...
	 * @param $uid
	 * @param $cid
	 * @param $id
	 */
	private function getPhotoPos($chapter_id){
		$pos = 0;$photolist = array();
		$photolist =  MemRow::getListId(array($this->photo, 'getPhotoListIds', array($chapter_id)));
		$pos = array_search($id,$photolist);
		return ($pos || $pos=== 0)?$pos+1:0;
	}
	
	/**
	 * 获取相片的详细信息
	 * Enter description here ...
	 * @param unknown_type $uid
	 * @param unknown_type $cid
	 * @param unknown_type $pos
	 */
	private function getPhotoInfoByPos($chapter_id,&$pos){
		$photolist =  MemRow::getListId(array($this->photo, 'getPhotoListIds', array($chapter_id)));
		reset($photolist);
		if($pos > count($photolist) || !$pos) {
			$pos = 1;			//如果是主题相册，则当pos为0时，将pos赋值为1，表示以一个相册的第一个照片的偏移量
		}
		$id = $photolist[$pos-1];
		
		return MemRow::mget(PHOTO_INFO,$id,array($this->photo,'getRows'));
	}
	
	/**
	 * 根据条件检索漫画列表
	 */
	public function search_action(){
		$key	=	urldecode(_('k')) ;
		$page 	=	_('page');
		$menu_title = '';
		$list=	$this->cartoon->search($key,$page,15);
		$this->ret = array('data'=>$list['ROWS'],'pages'=>$this->genPages($list['INFO'],array('keywords'=>$key)),'title' => $menu_title);
	}
	/**
	 * 根据条件检索漫画列表
	 *
	 */
	public function list_action(){
		$type 	= 	_('type');
		$key	=	_('k');
		$page 	=	_('page');
		$menu_title = '';
		if($key){
			if($type == TYPE_CATEGORY){
				$list	=	$this->cartoon->getCartoonList(array('ct_category'=>$key),$page,$pageSize = 10);
				$catelist	=	cartoon_util_interface::cacheGetCateList();
				$menu_title	= '分类：'.$catelist[$key]['cate_name'];
			}else{
				//根据tag搜索漫画列表
				$cartoon_ids = Model::factory('cartoon_model_tag')->getResList($key);
				//获取分页的相关信息
				list($clist,$pageinfo) = MemRow::getPageListId($cartoon_ids,$page,$pageSize);
				$list	=	array('ROWS' =>$this->cartoon->getAll($clist),'INFO'=>$pageinfo);
			}
		}
		
		$this->ret = array('data'=>$list['ROWS'],'pages'=>$this->genPages($list['INFO']),'title' => $menu_title);
	}
	/**
	 * 更多推荐页面
	 *
	 */
	public function recomlist_action(){
		$recom = _('recom');
		$page = _('page',1);
		switch ($recom) {
			case 1:
				$title = '强力推荐';
				break;
			case 2:
				$title = '热门推荐';
				break;
			default:
				$title = '最新上传';
				break;
		}
		
		$list = $this->cartoon->getCTListRecomPage($recom,$page,16);
		$this->ret = array('data'=>$list['ROWS'],'pages'=>$this->genPages($list['INFO']),'title' => $title);
		$this->view = 'list';
	}
	/**
	 * 更多分享列表
	 *
	 */
	public function news_action(){
		$page = _('page');
		$pageSize = 20;
		$uplist	=	$this->cartoon->getUpListPages($page,$pageSize);
		$pages = $this->genPages($uplist['INFO']);
		$this->ret = array('data'=>$pages,'pages'=>$pages);
	}
	
	public function view_news_action(){
		$id = _('id');
		$info = $this->cartoon->getUpLog($id);
		$this->ret = array('info'=>$info);
	}
	public function log_action(){
		index_util_interface::log(1,1,1,1);
	}
	
	/**
	 * 打分
	 *
	 */
	public function score_action(){
		$cartoon_id = _('resid');
		$score = _('score');
		$this->requireLogin();
		$uid	=	self::$login['uid'];
		$this->_renderJson();
		if($rs = $this->cartoon->score($cartoon_id,$uid,$score)){
			if($rs === true){
				$this->ret = self::success();
			}
			elseif($rs == Error::ERROR_RECORD_EXISTS){
				$this->ret = self::failure(Error::ERROR_RECORD_EXISTS);
			}else{
				$this->ret = self::failure(Error::ERROR_UNKNOWN );
			}
		}
		else $this->ret = self::failure('打分失败，请稍后再试！');
	}
}
?>